package com.shanghaionstar.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.EncryptUtil;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.presenter.OptPresenter;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * Created by Yonglang Hong on 2016/11/10.
 */
public class CarAssessAgreementDialog extends Dialog{

    CheckBox checkBox;

    TextView textView;

    View btn_gotoReport;

    BaseActivity activity;

    public CarAssessAgreementDialog(Context context) {
        super(context);
        init(context);
    }

    public CarAssessAgreementDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    public CarAssessAgreementDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void init(final Context context){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_car_assess);
        activity = (BaseActivity) context;
        checkBox = (CheckBox)findViewById(R.id.cb_car_assess);
        textView = (TextView) findViewById(R.id.tv_car_assess);
        btn_gotoReport = findViewById(R.id.btn_gotoReport);
        btn_gotoReport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carAssessmentAgreeListener.doOnAgree();
                if (checkBox.isChecked()) {
                    BusinessUtil.sendReportByClickType(getContext(), FunctionNameFindId.CarAssmt_EnterReportWithProtocolChecked.getStringValue());//爱车评估_勾选协议进入报告
                    new OptPresenter(activity.getScheduler(), activity).optIn(OptPresenter.CAR_ASSESSMENT);
                }else {
                    BusinessUtil.sendReportByClickType(getContext(), FunctionNameFindId.CarAssmt_EnterReportWithProtocolUnchecked.getStringValue());//爱车评估_未勾选协议进入报告
                }
            }
        });
//        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                btn_gotoReport.setEnabled(isChecked);
//            }
//        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Bundle bundle = new Bundle();
//                bundle.putString("className", CommonWebViewActivity.class.getName());
//                String linkUrl = EncryptUtil.decryptBase64(getContext(), Constants.PREFIX_H5_URL) + Constants.CAR_ASSESSMENT_AGREEMENT;
//                bundle.putString("linkUrl", linkUrl);//http://idt5.onstar.com.cn/mweb/mweb-main/static/OnStar-MA7.0-h5/html/car-assessment/car-inspection.html
//                bundle.putString("topTitle", getContext().getResources().getString(R.string.driver_assessment_terms_of_service));
//                bundle.putBoolean("canGoBack", true);
//                Intent intent = new Intent(context,CommonWebViewActivity.class);
//                intent.putExtras(bundle);
//                context.startActivity(intent);
                String linkUrl = Constants.CURRENT_PREFIX_H5_URL+ Constants.CAR_ASSESSMENT_AGREEMENT;
                WebDataEntity entity = new WebDataEntity();
                entity.setCanGoBack(true);
                entity.setTopTitle(getContext().getResources().getString(R.string.driver_assessment_terms_of_service));
                entity.setLinkUrl(linkUrl);
                entity.setWhat(IWebType.TYPE_NORMAL);
                CommonWebViewActivity.launchCommonWeb((Activity) context,entity);
                BusinessUtil.sendReportByClickType(getContext(), FunctionNameFindId.CarAssmt_ViewCarAssmtServiceProtocol.getStringValue());//爱车评估_未勾选协议进入报告
            }
        });
        Window window = getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        DisplayMetrics metrics = new DisplayMetrics();
        d.getMetrics(metrics);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        LogManager.getInstance().d("widthPixels" + metrics.widthPixels);
        p.width = (int) (metrics.widthPixels * 0.85);
        p.height = (int) (metrics.heightPixels * 0.7);
        window.setAttributes(p);
    }

    carAssessmentAgreeListener carAssessmentAgreeListener;

    public void setCarAssessmentAgreeListener(CarAssessAgreementDialog.carAssessmentAgreeListener carAssessmentAgreeListener) {
        this.carAssessmentAgreeListener = carAssessmentAgreeListener;
    }

    public interface carAssessmentAgreeListener{
        void doOnAgree();
    }

}
